import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { config } from 'dotenv';
import { ApiTags, DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { defaultConfig, setDefaultConfig } from '@game/config/config';
import {
  OpenAPIObject,
  OperationObject,
} from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
import { ApiConfig, ApiConfigKit } from 'tnwx';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';

// 加载配置文件
config();
setDefaultConfig();

process.env.MD5KEY = defaultConfig.MD5KEY;
process.env.aeskey = defaultConfig.aeskey;
// 初始小程序配置
const devApiConfig = new ApiConfig(
  defaultConfig.weixin.appid,
  defaultConfig.weixin.secret,
);
ApiConfigKit.putApiConfig(devApiConfig);

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  // 静态文件管理
  app.useStaticAssets('image', {
    prefix: '/public',
  });
  const env = process.env.NODE_ENV;
  console.log('====', env);
  if (env === 'dev') {
    const admin_options = new DocumentBuilder()
      .setTitle('rose api doc')
      .setDescription('The begonia api doc')
      .setVersion('1.0')
      .addBearerAuth()
      .build();
    app.enableCors(); // 允许跨域

    const document = SwaggerModule.createDocument(app, admin_options);
    SwaggerModule.setup('api', app, document);
  }

  console.log('http://127.0.0.1:' + defaultConfig.port);
  await app.listen(defaultConfig.port);
}
bootstrap();
